class Trie{
public:
    unordered_map<int,Trie*> next;
    Trie(){
            
    }
    void insert(int x){
        Trie* node = this;
        for(int i = 31;i >= 0;i--){
            int j = ((x >> i) & 1);
            if(!node->next.count(j)){
                node->next[j] = new Trie();
            }
            node = node->next[j];
        }
    }
                                        
    int getXOR(int x){
        Trie* node = this;
        int ans = 0;
        for(int i = 31;i >= 0;i--){
            int j = ((x >> i) & 1);
            int k;
            if(j == 1) k = 0;
            else k = 1;
                
            if(node->next.count(k)){
                node = node->next[k];
                ans += (1 << i);
            }else{
                node = node->next[j];
            }
        }
        return ans;
    }
};

class Solution {
public:                       
    Trie* t = new Trie();                      
    int findMaximumXOR(vector<int>& nums) {
        int n = nums.size();
        int ans = 0;
        for(int i = 0;i < n;i++){
            t->insert(nums[i]);
            ans = max(ans,t->getXOR(nums[i]));
        }

        return ans;
    }
};
